package com.ficus.road.maintain.authority.controller;

import com.ficus.road.maintain.authority.dto.UserDTO;
import com.ficus.road.maintain.authority.interfaces.PasswordUpdate;
import com.ficus.road.maintain.authority.interfaces.UserUpdate;
import com.ficus.road.maintain.authority.service.UserService;
import com.ficus.road.maintain.authority.vo.LoginResponse;
import com.ficus.road.maintain.authority.vo.UserQueryRequest;
import com.ficus.road.maintain.authority.vo.UserUpdateVO;
import com.ficus.road.maintain.authority.vo.UserVO;
import com.ficus.road.maintain.core.exception.BadRequestException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author xiawen yang
 * @date 2021/8/25 下午7:22
 */
@RestController
@RequestMapping("/authority")
public class UserController {

    @Autowired
    private UserService userService;

    //全量查询用户
    @PostMapping("/user")
    @PreAuthorize("@rm.check()")
    public ResponseEntity<List<UserVO>> getUsersByQuery(@RequestBody UserQueryRequest userQueryRequest) {
        Criteria criteria = new Criteria();
        if (Objects.nonNull(userQueryRequest.getAccount())) {
            criteria.and("account").is(userQueryRequest.getAccount());
        }
        if (Objects.nonNull(userQueryRequest.getUsername())) {
            criteria.and("username").is(userQueryRequest.getUsername());
        }
        if (Objects.nonNull(userQueryRequest.getRoleIds())) {
            criteria.and("role_id_list").in(userQueryRequest.getRoleIds());
        }
        if (Objects.nonNull(userQueryRequest.getCarIds())) {
            criteria.and("car_id_list").in(userQueryRequest.getCarIds());
        }
        Query query = new Query(criteria);
        List<UserVO> userVOS = userService.queryUsersByQuery(query);
        return new ResponseEntity<>(userVOS, HttpStatus.OK);
    }

    //查询用户权限
    @GetMapping("/users/{userId}")
    public ResponseEntity<LoginResponse> getUsersByQuery(@PathVariable("userId") String userId) {
        if (StringUtils.isEmpty(userId)) {
            throw new BadRequestException("Bad Request, userId is invalid");
        }
        UserDTO userDTO = userService.queryUserById(userId);
        if (Objects.isNull(userDTO)) {
            throw new BadRequestException("Bad Request, userId is invalid");
        }
        LoginResponse loginResponse = userService.queryLoginInfoByUsername(userDTO.getAccount());
        return new ResponseEntity<>(loginResponse, HttpStatus.OK);
    }

    //创建用户
    @PutMapping("/user")
    @PreAuthorize("@rm.check()")
    public ResponseEntity<String> createUser(@Validated @RequestBody UserVO user) {
        String userId = userService.createUser(user);
        return new ResponseEntity<>(userId, HttpStatus.OK);
    }

    //更新用户
    @PatchMapping("/users/{userId}")
    @PreAuthorize("@rm.check()")
    public ResponseEntity<String> updateUser(@PathVariable("userId") String userId,
                                             @Validated(UserUpdate.class) @RequestBody UserUpdateVO userUpdateVO) {
        String id = userService.updateUser(userId, userUpdateVO);
        return new ResponseEntity<>(id, HttpStatus.OK);
    }

    //删除用户
    @DeleteMapping("users/{userId}")
    @PreAuthorize("@rm.check()")
    public ResponseEntity<String> deleteUser(@PathVariable("userId") String userId) {
        String id = userService.deleteUser(userId);
        return new ResponseEntity<>(id, HttpStatus.OK);
    }

    @PostMapping("users/password/{userId}")
    public ResponseEntity<String> resetPassWord(@PathVariable("userId") String userId,
                                                @Validated(PasswordUpdate.class) @RequestBody UserUpdateVO userUpdateVO) {
        String id = userService.updatePassword(userId, userUpdateVO);
        return new ResponseEntity<>(id, HttpStatus.OK);
    }
}
